using Plots
using BenchmarkTools
This is the Julia version of the
@inline function friction_fn(v::Float64, vt::Float64)
if v > vt
return - v * 3
else
return - vt * 3 * sign(v)
end
end
function simulate_spring_mass_funky_damper(x0, T=10, dt=0.0001, vt=1.0)
times::Array{Float64, 1} = [t for t in range(0, T, step=dt)]
positions::Array{Float64, 1} = zeros(Float64, size(times))
v::Float64 = 0.
a::Float64 = 0.
x::Float64 = x0
@inbounds positions[1] = 1
for ii in 2:length(times)
@inbounds t = times[ii]
a = friction_fn(v, vt) - 100. *x
v = v + a*dt
x = x + v*dt
@inbounds positions[ii] = x/x0
end
return times, positions
end
simulate_spring_mass_funky_damper (generic function with 4 methods)
plot(simulate_spring_mass_funky_damper(0.1), label=0.1)
plot!(simulate_spring_mass_funky_damper(1), label=1)
plot!(simulate_spring_mass_funky_damper(10), label=10)
@benchmark simulate_spring_mass_funky_damper(1.)
BenchmarkTools.Trial: memory estimate: 1.53 MiB allocs estimate: 4 -------------- minimum time: 834.015 μs (0.00% GC) median time: 1.365 ms (0.00% GC) mean time: 1.395 ms (7.99% GC) maximum time: 6.649 ms (77.68% GC) -------------- samples: 3567 evals/sample: 1